<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('修改种养殖技术')"/>
    <th:block th:include="include :: datetimepicker-css"/>

    <th:block th:include="include :: select2-css"/>
    <th:block th:include="include :: bootstrap-fileinput-css"/>
    <th:block th:include="include :: summernote-css"/>

</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-growtech-edit" th:object="${growTech}">
            <input name="growId" th:field="*{growId}" type="hidden">
            <div class="row">
                <div class="col-sm-6">

                    <div class="form-group">
                        <label class="col-sm-3 control-label">标题：</label>
                        <div class="col-sm-8">
                            <input name="title" th:field="*{title}" class="form-control" type="text">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">简介：</label>
                        <div class="col-sm-8">
                            <textarea name="introduction" class="form-control">[[*{introduction}]]</textarea>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">类型：</label>
                        <div class="col-sm-8">
                            <select name="shapeType" id="shapeType" th:field="*{shapeType}" class="form-control m-b" onchange="courseTypeChange()">
                                <option value="Image">图文</option>
                                <option value="Video">视频</option>
                            </select>
                        </div>
                    </div>




                </div>

                <div class="col-sm-6">

                    <!--  不需要版块
                    <div class="form-group">
                        <label class="col-sm-3 control-label">版块：</label>
                        <div class="col-sm-8">

                            <div class="input-group">
                                <input id="columnDictvalue" name="columnDictvalue" type="hidden" value="0"  th:field="*{columnDictvalue}" />
                                <input id="sectionDictreeId" name="sectionDicParentId" type="hidden" />
                                <input class="form-control" type="text" onclick="selectSectionDictTree()" id="sectionDicTreeName" readonly="true" value="所有">
                                <span class="input-group-addon"><i class="fa fa-search"></i></span>
                            </div>

                        </div>
                    </div>
                    -->

                    <div class="form-group">
                        <label class="col-sm-3 control-label">发布时间：</label>
                        <div class="col-sm-8">
                            <div class="input-group date">
                                <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                                <input name="issueTime" th:value="${#dates.format(growTech.issueTime, 'yyyy-MM-dd')}"  class="form-control" placeholder="yyyy-MM-dd" type="text">
                            </div>
                        </div>
                    </div>


                    <div class="form-group">
                        <label class="col-sm-3 control-label">作物：</label>
                        <div class="col-sm-8">
                            <div class="input-group">

                                <input id="cropsDictvalue" name="cropsDictvalue" type="hidden" th:field="*{cropsDictvalue}" />

                                <input id="treeId" name="parentId" type="hidden" value="0" />
                                <input class="form-control" type="text" onclick="selectCropDictTree()" id="treeName" readonly="true" value="所有">
                                <span class="input-group-addon"><i class="fa fa-search"></i></span>
                            </div>
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-3 control-label"><span style="color: red; ">*</span>主图上传：</label>
                        <div class="col-sm-8">

                            <div class="layui-upload">
                                <input id="upload" name="file" type="file">
                            </div>
                            <input id="mainImg" name="mainImg"  type="hidden">

                        </div>
                    </div>

                </div>
            </div>

            <div class="row">
                <div class="col-sm-11">
                    <div class="form-group" id="spdiv">
                        <label class="col-sm-2 control-label">培训视频：</label>
                        <div class="col-sm-9">
                            <input id="videoUrl" name="videoUrl" class="form-control"  type="text">

                            <div class="layui-upload">
                                <input id="uploadVideo" name="file" type="file">
                            </div>

                        </div>
                    </div>
                </div>

                <div class="form-group" id="twdiv">
                    <div class="col-sm-12">
                        <label class="col-sm-2 control-label">图文详情：</label>
                        <!-- 富文本编辑框-->
                        <div class="col-sm-10">

                            <div id="editor" class="summernote"></div>
                            <input type="hidden" name="content" id="content"  th:field="*{content}" value="value">

                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-sm-offset-6 col-sm-10">
                        <button type="button" class="btn btn-sm btn-primary" onclick="submitHandler()"><i
                                class="fa fa-check"></i>保存种养殖技术信息
                        </button>
                    </div>
                </div>
            </div>

        </form>
    </div>
    <th:block th:include="include :: footer"/>
    <th:block th:include="include :: datetimepicker-js"/>

    <th:block th:include="include :: select2-js"/>
    <th:block th:include="include :: bootstrap-fileinput-js"/>
    <th:block th:include="include :: summernote-js"/>

    <script th:inline="javascript">
        var ctx = [[${#request.getContextPath()}]];
        var prefix = ctx + "/agriculture/growtech";
        var croptreeprefix = ctx + "/agriculture/cropDict";
        var sectionDictprefix = ctx + "/agriculture/sectionDict";

        $("#form-growtech-edit").validate({
            focusCleanup: true
        });

        $("#treeId").val($("#cropsDictvalue").val());

        /*作物字典-新增-选择父部门树*/
        function selectCropDictTree() {
            var options = {
                title: '作物字典选择',
                width: "380",
                url: croptreeprefix + "/selectCropDictTree/" + $("#treeId").val(),
                callBack: doSubmit
            };
            $.modal.openOptions(options);
        }

        function doSubmit(index, layero){
            var body = layer.getChildFrame('body', index);
            $("#treeId").val(body.find('#treeId').val());
            $("#cropsDictvalue").val(body.find('#treeId').val());

            $("#treeName").val(body.find('#treeName').val());
            layer.close(index);
        }

        function initCrop() {
            var cropsDictvalue=$("#cropsDictvalue").val();
            $.ajax({
                url: prefix + "/selectCropDictById",
                type: "POST",
                data: {cropId:cropsDictvalue},
                dataType: "json",
                async: false,
                success: function (res) {
                    console.log(res);
                    console.log(res.data.cropName);
                    $("#treeName").val(res.data.cropName);
                }
            })
        }

        initCrop();


        $("#sectionDictreeId").val($("#columnDictvalue").val());

        /*板块字典-新增-选择父部门树*/
        function selectSectionDictTree() {
            var options = {
                title: '板块字典选择',
                width: "380",
                url: sectionDictprefix + "/selectSectionDictTree/" + $("#sectionDictreeId").val(),
                callBack: doSectionSubmit
            };
            $.modal.openOptions(options);
        }

        function doSectionSubmit(index, layero){
            var body = layer.getChildFrame('body', index);
            console.log(body);
            console.log(body.find('#treeId').val());
            console.log(body.find('#treeName').val());

            $("#sectionDictreeId").val(body.find('#treeId').val());
            $("#columnDictvalue").val(body.find('#treeId').val());
            $("#sectionDicTreeName").val(body.find('#treeName').val());

            layer.close(index);
        }


        function initSection() {
            var columnDictvalue=$("#columnDictvalue").val();
            $.ajax({
                url: prefix + "/selectSectionDictById",
                type: "POST",
                data: {sectionId:columnDictvalue},
                dataType: "json",
                async: false,
                success: function (res) {
                    console.log(res);
                    console.log(res.data.sectionName);
                    $("#sectionDicTreeName").val(res.data.sectionName);
                }
            })
        }

        //initSection();


        $(document).ready(function () {

            let images = [];
            let image = [[${growTech.mainImg}]];

            if (image !== '-') {
                images.push(image);
            }

            $("#upload").fileinput({
                theme: 'explorer-fas',
                uploadUrl: ctx + "/oss/upload",
                enctype: 'multipart/form-data',
                overwriteInitial: true,
                initialPreviewAsData: true,
                //图片最大20M
                maxFileSize: 1024 * 20,
                allowedFileTypes: ['image'],    // allow only images
                initialPreview: images
            }).on('fileuploaded', function (event, data) {
                data = data.response;
                if (data.code !== 0) {
                    console.error(data.msg);
                } else {
                    $("#mainImg").val(data.data);
                }
            }).on('fileuploaderror', function (event, data, msg) {
                console.log('File Upload Error', 'ID: ' + data.fileId + ', Thumb ID: ' + data.previewId);
            }).on('filebatchuploadcomplete', function (event, preview, config, tags, extraData) {
                console.log('File Batch Uploaded', preview, config, tags, extraData);
            });
        });


        $(document).ready(function () {
            $("#videoUrl").val([[${growTech.videoUrl}]]);
            let videos = [];
            let video = [[${growTech.videoUrl}]];

            if (video !== '-') {
                videos.push(video);
            }

            $("#uploadVideo").fileinput({
                theme: 'explorer-fas',
                uploadUrl: ctx + "/oss/upload",
                enctype: 'multipart/form-data',
                overwriteInitial: true,
                initialPreviewAsData: true,
                //图片最大200M
                maxFileSize: 1024 * 200,
                allowedFileTypes: ['video'],
                initialPreviewFileType: ['video'],
                initialPreview: videos
            }).on('fileuploaded', function (event, data) {
                data = data.response;
                if (data.code !== 0) {
                    console.error(data.msg);
                } else {
                    $("#videoUrl").val(data.data);
                    //$("#mainImg").val(data.data);
                }
            }).on('fileuploaderror', function (event, data, msg) {
                console.log('File Upload Error', 'ID: ' + data.fileId + ', Thumb ID: ' + data.previewId);
            }).on('filebatchuploadcomplete', function (event, preview, config, tags, extraData) {
                console.log('File Batch Uploaded', preview, config, tags, extraData);
            });
        });

        $(function () {
            $('.summernote').summernote({
                placeholder: '请输入公告内容',
                height: 192,
                lang: 'zh-CN',
                followingToolbar: false,
                callbacks: {
                    onImageUpload: function (files) {
                        sendFile(files[0], this);
                    }
                }
            });
            var content = $("#content").val();
            $('#editor').summernote('code', content);
        });


        // 上传文件
        function sendFile(file, obj) {
            var data = new FormData();
            data.append("file", file);
            $.ajax({
                type: "POST",
                url: ctx + "/oss/upload",
                data: data,
                cache: false,
                contentType: false,
                processData: false,
                dataType: 'json',
                success: function (result) {
                    if (result.code == 0) {
                        $(obj).summernote('editor.insertImage', result.data, result.fileName);
                    } else {
                        $.modal.alertError(result.msg);
                    }
                },
                error: function (error) {
                    $.modal.alertWarning("图片上传失败。");
                }
            });
        }


        //根据下拉选择切换视频培训，还是图文培训
        courseTypeChange();

        function courseTypeChange() {
            var shapeTypepre = $("#shapeType").val()
            if (shapeTypepre == "Video") {
                $("#twdiv").hide();//隐藏
                $("#spdiv").show();
            } else if (shapeTypepre == "Image") {
                $("#spdiv").hide();
                $("#twdiv").show();
            }
        }


        function submitHandler() {
            if ($.validate.form()) {
                var sHTML = $('.summernote').summernote('code');
                $("#content").val(sHTML);
                $.operate.save(prefix + "/edit", $('#form-growtech-edit').serialize());
            }
        }

        $("input[name='issueTime']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });
    </script>
</body>
</html>
